package com.edu.framework.websocket.handler;

import cn.hutool.core.util.StrUtil;
import com.edu.framework.websocket.WebSocketHolder;
import com.edu.framework.websocket.message.base.Response;
import com.edu.framework.websocket.message.canteen.CanteenCntRequest;
import com.edu.framework.websocket.service.CanteenService;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

@Component
public class CanteenCntRequestHandler implements IHandler<CanteenCntRequest> {

    @Resource
    private CanteenService wsCanteenService;

    @Override
    public void execute(WebSocketSession session, CanteenCntRequest message) {
        if (message == null) {
            return;
        }
        Map<String, String> data = message.getData();
        String canteenId = data.get(CanteenCntRequest.CANTEEN_ID);
        Response response = null;

        if (StrUtil.isBlank(canteenId)){
            response = Response.fail(CanteenCntRequest.TYPE, "canteenId不能为空");
        } else {
            response = wsCanteenService.getCnt(Long.parseLong(canteenId));
        }

        try {
            WebSocketHolder.send(session,response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getType() {
        return CanteenCntRequest.TYPE;
    }




}
